package com.alm.token.ui.viewpagefragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.R;
import com.alm.token.base.BaseAdapter;
import com.alm.token.base.BaseViewPageFragment;
import com.alm.token.common.RefreshLoadMoreManager;
import com.alm.token.ui.activity.BlockWalletLoginActivity;
import com.alm.token.ui.activity.PayPwdActivity;
import com.alm.token.ui.data.bean.VipOrderListData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.VipApplyView;
import com.alm.token.utils.CommonUtil;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class VipOredrListPageFrag extends BaseViewPageFragment implements VipApplyView<Object> {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.tv_clickreload)
    TextView tvClickreload;
    @BindView(R.id.nodatashow_body)
    RelativeLayout nodatashowBody;
    Unbinder unbinder;
    private RefreshLoadMoreManager<VipOrderListData.DataBeanX.DataBean> manager;

    private List<VipOrderListData.DataBeanX.DataBean> dataBeans = new ArrayList<>();

    private int limit = 100, page = 1;
    private boolean isfull = false;

    public static VipOredrListPageFrag newInstance() {
        VipOredrListPageFrag fragment = new VipOredrListPageFrag();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected MyPreenter initPreenter() {
        return preenter = new MyPreenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.viewpage_fragment_message_base;
    }

    @Override
    protected void initVariables() {

    }

    @Override
    protected void initViews(View view) {
        initManager();
        swipeRefreshLayout.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {

    }

    private void initManager() {
        MessageAdapter adapter = new MessageAdapter(getActivity());
        manager = new RefreshLoadMoreManager.Builder(getActivity())
                .setAdapter(adapter)
                .setRecyclerView(recyclerView)
                .setOnListItemClickListener(new RefreshLoadMoreManager.OnListItemClickListener() {
                    @Override
                    public void onListItemClick(BaseAdapter.BaseItem item, int position) {
//                        if (dataBeans != null && dataBeans.size() > position) {
//                            Intent intent = new Intent(getContext(), QuantitativeOrderDetailActivity.class);
//                            intent.putExtra("databean", dataBeans.get(position));
//                            startActivity(intent);
//                        }

                    }
                })
                .setSwipeRefreshLayout(swipeRefreshLayout)
                .setOnRefreshListener(new RefreshLoadMoreManager.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
//                        showToast("onRefresh");
                        swipeRefreshLayout.setRefreshing(false);
                        loadData();
                    }
                })
                .setOpenLoadMore(true)
                .setOnLoadMoreListener(new RefreshLoadMoreManager.OnLoadMoreListener() {
                    @Override
                    public void onLoadMore() {
                        manager.getAdapter().loadMoreEnd(true);
                        manager.getAdapter().loadMoreFail();
                        showWaitDialog();
                        preenter.getVipOrderListData(String.valueOf(limit), String.valueOf(page));
                    }
                })
                .build();
    }

    @Override
    protected void loadData() {
        limit = 100;
        page = 1;
        preenter.getVipOrderListData(String.valueOf(limit), String.valueOf(page));
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void getData(Object o) {

    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            getActivity().sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            getActivity().finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(getActivity(), PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType", PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            getActivity().finish();
            showToSetPaypswDialog();
        }else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void onError(String error) {

        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {

        showWaitDialog();
    }

    @Override
    public void hideProgress() {

        hideWaitDialog();
    }

    private List<VipOrderListData.DataBeanX.DataBean> dataBeansOld = new ArrayList<>();

    @Override
    public void getVipOrderListData(Object vipOrderListData) {
        if (vipOrderListData instanceof VipOrderListData) {
            VipOrderListData data = (VipOrderListData) vipOrderListData;

            if (data.getStatus() == 0 && data.getData() != null && data.getData().getData() != null) {


                if (page == 1) {
                    dataBeans.clear();
                } else if (!isfull && dataBeans.size() > (page - 1) * 100) {
                    dataBeans.removeAll(dataBeansOld);
                }

                if (data.getData().getData().size() == 100) {
                    isfull = true;
                    page = page + 1;
                } else {
                    isfull = false;
                }

                dataBeansOld = data.getData().getData();
                dataBeans.addAll(data.getData().getData());
                for (int i = 0; i < dataBeans.size(); i++) {
                    dataBeans.get(i).setLayoutId(MessageAdapter.LAYOUT_ID);
                }
                manager.setData(dataBeans);
            } else showToast(data.getMsg());
        }

        if (dataBeans.size() == 0) {
            swipeRefreshLayout.setVisibility(View.GONE);
            nodatashowBody.setVisibility(View.VISIBLE);
        } else {
            swipeRefreshLayout.setVisibility(View.VISIBLE);
            nodatashowBody.setVisibility(View.GONE);
        }
    }

    @Override
    public void getCurrentOrderData(Object o) {

    }

    @Override
    public void getStopVipData(Object o) {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.tv_clickreload)
    public void onViewClicked() {
        loadData();
    }


    public class MessageAdapter extends BaseAdapter<VipOrderListData.DataBeanX.DataBean> {

        public static final int LAYOUT_ID = R.layout.item_message_base;

        public MessageAdapter(@NonNull Context context) {
            super(context);
        }

        @Override
        protected void convert(BaseViewHolder helper, VipOrderListData.DataBeanX.DataBean item, int itemType) {

            /**
             * id : 21
             * vipName : 超级会员
             * vipLevel : 3
             * createTime : 1546594886000
             * updateTime : null
             * status : 1
             * userId : 110
             * money : 10000
             * startTime : 1548754886000
             * endTime : null
             * lohasUser : {"unionid":null,"loginname":"test","loginpwd":null,"phone":"13428284493","wxopenid":null,"addtime":null,"updatetime":null,"membername":null,"cardid":null,"usertype":null,"token":null,"wxopenid2":null,"code":null,"headurl":null,"paypwd":null,"suanli":null,"ranking":null,"wabao":null,"duobao":null,"isduobao":null,"steallevel":null,"id":null}
             */

            int status = item.getStatus();

            helper.setText(R.id.title, item.getVipName());
            helper.setText(R.id.message, status == 1 ? getString(R.string.vip_hint_staue1) : status == 2 ? getString(R.string.vip_hint_staue2) : status == 3 ? getString(R.string.vip_hint_staue3) :
                    status == 4 ? getString(R.string.vip_hint_staue4) : getString(R.string.vip_hint_staue5));
            helper.setText(R.id.date, CommonUtil.getDateTimeFromMillisecond(item.getCreateTime()).split(" ")[0]);
            if (item.getEndTime() != 0L) {
                helper.setVisible(R.id.tv_time0, true);
                helper.setText(R.id.tv_time0, CommonUtil.getDateTimeFromMillisecond(item.getEndTime()).split(" ")[0]);
            } else
                helper.setVisible(R.id.tv_time0, false);

//            helper.setText(R.id.message, item.getContent());

//            helper.setText(R.id.title, "借款成功");
//            helper.setText(R.id.date, "2017-05-24");
//            helper.setText(R.id.message, "文字文字文字文字文字文字文字文字文字文字文字文字文字文字");
        }

    }
}
